package com.ywu.role.standard.service;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.ywu.boot.context.YwuContext;
import com.ywu.boot.context.YwuContextHolder;
import com.ywu.common.entity.PageList;
import com.ywu.common.page.PageService;
import com.ywu.common.result.CommonResult;
import com.ywu.role.standard.data.ApiRoleAuthRepository;
import com.ywu.role.standard.data.MenuRoleAuthRepository;
import com.ywu.role.standard.data.UserRoleAuthRepository;
import com.ywu.role.standard.data.UserRoleRepository;
import com.ywu.role.standard.dto.UserRoleAuthDto;
import com.ywu.role.standard.dto.UserRoleDto;
import com.ywu.role.standard.entity.UserRoleEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * @ClassName UserRoleService
 * @Description TODO
 * @Author GroundDemo
 * @Date 2024/3/9 16:08
 * @Version 1.0
 **/
@Service
public class UserRoleService {
    @Autowired
    private UserRoleRepository userRoleRepository;

    @Autowired
    private UserRoleAuthRepository userRoleAuthRepository;

    @Autowired
    private MenuRoleAuthRepository menuRoleAuthRepository;

    @Autowired
    private ApiRoleAuthRepository apiRoleAuthRepository;

    /**
    * @Author GroundDemo
    * @Description 添加角色信息
    * @Date 16:10 2024/3/9
    * @Param [userRoleDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> createUserRole(UserRoleDto userRoleDto) {
        UserRoleEntity userRoleEntity = new UserRoleEntity();
        BeanUtils.copyProperties(userRoleDto, userRoleEntity);
        // 未传角色编码时生成随机UUID
        if (StringUtils.isBlank(userRoleDto.getRoleCode())) {
            String uuid = UUID.randomUUID().toString();
            String roleCode = uuid.replace("-", "");
            userRoleEntity.setRoleCode(roleCode);
        }
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        userRoleEntity.createInfo(currentContext.getYwuContextUser().getUserAccount());
        userRoleEntity.setTenantId(currentContext.getYwuContextTenant().getTenantId());
        userRoleRepository.createUserRole(userRoleEntity);
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 修改角色信息
    * @Date 16:27 2024/3/9
    * @Param [userRoleDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> updateUserRole(UserRoleDto userRoleDto) {
        UserRoleEntity userRoleEntity = new UserRoleEntity();
        BeanUtils.copyProperties(userRoleDto, userRoleEntity);
        // 未传角色编码时生成随机UUID
        if (StringUtils.isBlank(userRoleDto.getRoleCode())) {
            String roleCode = UUID.randomUUID().toString().replace("-", "");
            userRoleEntity.setRoleCode(roleCode);
        }
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        userRoleEntity.updateInfo(currentContext.getYwuContextUser().getUserAccount());
        userRoleRepository.updateUserRole(userRoleEntity);
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 删除角色
    * @Date 16:30 2024/3/9
    * @Param [userRoleDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> deleteUserRole(UserRoleDto userRoleDto) {
        UserRoleEntity userRoleEntity = new UserRoleEntity();
        BeanUtils.copyProperties(userRoleDto, userRoleEntity);
        userRoleRepository.deleteUserRole(userRoleEntity);
        // 删除角色同时删除授权相关信息 包含 菜单授权、api授权、用户授权
        userRoleAuthRepository.deleteByRoleId(userRoleDto.getId());
        menuRoleAuthRepository.deleteByRoleCode(userRoleDto.getRoleCode());
        apiRoleAuthRepository.deleteByRoleId(userRoleDto.getId());
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 查询角色列表
    * @Date 16:32 2024/3/9
    * @Param [userRoleDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> queryUserRole(UserRoleDto userRoleDto) {
        PageService.execPageStartAndEnd(userRoleDto.getPageInfo());
        int count = userRoleRepository.queryUserRoleCount(userRoleDto);
        List<UserRoleEntity> entityList = userRoleRepository.queryUserRole(userRoleDto);
        PageList<UserRoleEntity> userRoleEntityPageList = new PageList<>(userRoleDto.getPageInfo().getPageIndex(),
                userRoleDto.getPageInfo().getPageSize(), count, entityList);
        return CommonResult.success(userRoleEntityPageList);
    }
}
